package servlet;

import bean.User;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class CheckUserServlet2 extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收jquery发送的ajax数据
        String username = req.getParameter("uname");
        System.out.println("jquery ajax username = " + username);
        resp.setContentType("text/html;charset=utf-8");
        if ("king".equals(username)) {
            User user = new User(100, "king", "king@sohu.com", "aaa");
            resp.getWriter().write(new Gson().toJson(user));
        }else {
            //返回一个不存在的user
            User user = new User(-1, "", "", "");
            String json = new Gson().toJson(user);
            resp.getWriter().write(json);
        }

    }
}
